using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace FoundationLib.AddIns.Constraints.Visitors
{
	/// <summary>
	/// Represents a visitor evaluating OCL infix notation statements
	/// </summary>
	public class InfixVisitor : RegexVisitor
	{
		private const string PATTERN = @"([^ ]+)\.([^\(.]+)\(([^\)]{0,})\)";

		/// <summary>
		/// Creates a new infix visitor
		/// </summary>
		public InfixVisitor()
			: base(PATTERN)
		{
		}

		private string Convert(Match m)
		{
			switch(m.Groups[2].Value)
			{
				case "concat":
					return String.Format("String.Concat({0}, {1})", m.Groups[1].Value, m.Groups[3].Value);
				case "size":
					return m.Groups[1].Value + ".Length";
				case "toLower":
					return m.Groups[1].Value + ".ToLower()";
				case "toUpper":
					return m.Groups[1].Value + ".ToUpper()";
				case "substring":
					return m.Groups[1].Value + ".Substring(" + m.Groups[3].Value + ")";
				case "notEmpty":
					return "!String.IsNullOrWhiteSpace(" + m.Groups[1].Value + ")";
				case "isEmpty":
					return "String.IsNullOrWhiteSpace(" + m.Groups[1].Value + ")";
				default:
					return m.Groups[0].Value;
			}
		}

		/// <summary>
		/// Evaluates regular expession matches
		/// </summary>
		/// <returns>The result of the evaluation</returns>
		/// <param name="match">The regular expression match</param>
		protected override string Evaluate(Match match)
		{
			return this.Convert(match);
		}
	}
}
